package com.ruoyi.hospital.order.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.hospital.hospitalized.domain.THospitalized;
import com.ruoyi.hospital.hospitalized.service.ITHospitalizedService;
import com.ruoyi.hospital.medicine.domain.TPatientMedicine;
import com.ruoyi.hospital.medicine.service.ITPatientMedicineService;
import com.ruoyi.hospital.order.domain.TOrder;
import com.ruoyi.hospital.order.service.ITOrderService;
import com.ruoyi.hospital.registered.domain.TRegistered;
import com.ruoyi.hospital.registered.service.ITRegisteredService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 订单管理Controller
 *
 * @author Shawn
 * @date 2024-02-22
 */
@Controller
@RequestMapping("/hospital/order")
public class TOrderController extends BaseController {
    private String prefix = "hospital/order";

    @Autowired
    private ITOrderService tOrderService;

    @Autowired
    private ITRegisteredService itRegisteredService;

    @Autowired
    private ITHospitalizedService itHospitalizedService;

    @Autowired
    private ITPatientMedicineService itPatientMedicineService;

    @RequiresPermissions("hospital:order:view")
    @GetMapping()
    public String order() {
        return prefix + "/order";
    }


    @GetMapping("/detail/{id}")
    public String detail(@PathVariable("id") String id, ModelMap modelMap) {
        TOrder tOrder = tOrderService.selectTOrderById(Long.parseLong(id));
        if (null == tOrder) {
            throw new ServiceException("订单不存在");
        }

        modelMap.addAttribute("tOrder", tOrder);
        //门诊收费
        if ("1".equals(tOrder.getType())) {
            TRegistered registered = itRegisteredService.selectTRegisteredById(Long.parseLong(tOrder.getRegisteredId()));
            modelMap.addAttribute("registered", registered);
        } else if ("2".equals(tOrder.getType())) {
            THospitalized tHospitalized = itHospitalizedService.selectTHospitalizedById(Long.parseLong(tOrder.getHospitalizedId()));
            modelMap.addAttribute("tHospitalized", tHospitalized);
        } else if ("3".equals(tOrder.getType())) {
            TRegistered registered = itRegisteredService.selectTRegisteredById(Long.parseLong(tOrder.getRegisteredId()));
            modelMap.addAttribute("registered", registered);
            List<TPatientMedicine> tPatientMedicineList = itPatientMedicineService.queryByregistered(tOrder.getRegisteredId());
            modelMap.addAttribute("tPatientMedicineList", tPatientMedicineList);
        }
        return prefix + "/detail";
    }

    /**
     * 查询订单管理列表
     */
    @RequiresPermissions("hospital:order:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TOrder tOrder) {
        startPage();
        List<TOrder> list = tOrderService.selectTOrderList(tOrder);
        return getDataTable(list);
    }

    /**
     * 导出订单管理列表
     */
    @RequiresPermissions("hospital:order:export")
    @Log(title = "订单管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TOrder tOrder) {
        List<TOrder> list = tOrderService.selectTOrderList(tOrder);
        ExcelUtil<TOrder> util = new ExcelUtil<TOrder>(TOrder.class);
        return util.exportExcel(list, "订单管理数据");
    }

    /**
     * 新增订单管理
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存订单管理
     */
    @RequiresPermissions("hospital:order:add")
    @Log(title = "订单管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(TOrder tOrder) {
        return toAjax(tOrderService.insertTOrder(tOrder));
    }

    /**
     * 修改订单管理
     */
    @RequiresPermissions("hospital:order:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        TOrder tOrder = tOrderService.selectTOrderById(id);
        mmap.put("tOrder", tOrder);
        return prefix + "/edit";
    }

    /**
     * 修改保存订单管理
     */
    @RequiresPermissions("hospital:order:edit")
    @Log(title = "订单管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TOrder tOrder) {
        return toAjax(tOrderService.updateTOrder(tOrder));
    }

    /**
     * 删除订单管理
     */
    @RequiresPermissions("hospital:order:remove")
    @Log(title = "订单管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(tOrderService.deleteTOrderByIds(ids));
    }
}
